/**
 * Created by Cake on 2017/6/13.
 */

// alert消息框
var alertMsg = function(msg, onAfterHide) {
    var alertDOM,
        list = document.getElementsByClassName('alert');

    var handleAnimation = function(animation) {
        if (animation.animationName === "hide") {
            destroy();
            if (typeof onAfterHide == 'function') {
                onAfterHide();
            }
        }
    };

    var create = function () {
        alertDOM = document.createElement('div');
        alertDOM.setAttribute('font', '12');
        alertDOM.setAttribute('flex', 'cross:center main:center');
        alertDOM.className = 'alert';
        alertDOM.innerHTML += '<div class="alert-con">'+ msg + '</div>';

        alertDOM.addEventListener("webkitAnimationEnd", handleAnimation, false);
        alertDOM.addEventListener("msAnimationEnd", handleAnimation, false);
        alertDOM.addEventListener("oAnimationEnd", handleAnimation, false);
        alertDOM.addEventListener("animationend", handleAnimation, false);

        document.body.appendChild(alertDOM);
        alertDOM.className += ' alert-show';

        setTimeout(function () {
            hide();
        }, 3000);

        del();
    };
    
    var hide = function () {
        alertDOM.className = alertDOM.className.replace("show", "hide");
    };
    
    var destroy = function () {
        document.body.removeChild(alertDOM);
    };
    
    var del = function () {
        if (list.length > 1) {
            document.body.removeChild(list[0]);
        }
    };

    create();
};

// confirm消息框
var confirmMsg = function(msg, callback) {
    var arr = [];
    arr.push('<div class="dialog-msg dialog-confirm">');
    arr.push('<div class="confirm">');
    arr.push('<div class="confirm-box">');
    arr.push('<div class="confirm-text" font="16">'+ msg +'</div>');
    arr.push('<div class="confirm-opt" font="14" flex="box:mean">');
    arr.push('<a href="javascript:;" class="quite">取消</a>');
    arr.push('<a href="javascript:;" class="sure">确定</a>');
    arr.push('</div></div></div></div>');
    $('body').append(arr.join(''));

    setTimeout(function () {
        $('.dialog-confirm').addClass('dialog-show');
        $('.confirm-box').addClass('confirm-box-normal');
    }, 20);

    $('.quite').click(function () {
        $('.dialog-confirm').removeClass('dialog-show');
        $('.confirm-box').removeClass('confirm-box-normal');
        if (typeof (callback) == 'function') {
            callback(false);
        }
        setTimeout(function () {
            $('.dialog-confirm').remove();
        }, 300)
    });

    $('.sure').click(function () {
        $('.dialog-confirm').removeClass('dialog-show');
        $('.confirm-box').removeClass('confirm-box-normal');
        if (typeof (callback) == 'function') {
            callback(true);
        }
        setTimeout(function () {
            $('.dialog-confirm').remove();
        }, 300)
    });
};